<?php

namespace app\admin\model;


use think\Cache;
use think\Model;
use think\Session;

class Admin extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected static function init()
    {
        self::afterWrite(function ($row) {

        });
    }


    /**
     * 重置用户密码
     * @author baiyouwen
     */
    public function resetPassword($uid, $NewPassword)
    {
        $passwd = $this->encryptPassword($NewPassword);
        $ret = $this->where(['id' => $uid])->update(['password' => $passwd]);
        return $ret;
    }

    // 密码加密
    protected function encryptPassword($password, $salt = '', $encrypt = 'md5')
    {
        return $encrypt($password . $salt);
    }

    public function manystoreshop()
    {
        return $this->belongsTo('StoreShop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function sellerconfig()
    {
        return $this->belongsTo(SellerConfig::class, 'config_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    //获取代理的下级
    public function getAgentChild($uid, $type = 'id')
    {

        $auth_level = (new AuthGroupAccess())->where('uid', $uid)->find();
        if ($auth_level->group_id == get_addon_config('douyin')['agent_id']) {
            $child = $this->where(['pid' => $uid])->column($type);
            if($type=="shop_id") {
                return $child;
            }
            return array_merge([$uid], $child);
        }

        return null;

    }


    public function getAgentSum($uid)
    {

        if ($uid != get_addon_config('douyin')['super_id']) {
            $child = $this->getAgentChild($uid, 'shop_id');
            if ($child) {
                $map['manystore_id'] = ['in', $child];
            } else {
                $shop_id = Session::get("admin")['shop_id'];
                $map['manystore_id'] = $shop_id;
            }
            $totaluser = (new User())->where($map)->count();
            $totalshop = 0;
            $totalactivity = (new Activity())->where($map)->count();
            $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
            $map['createtime'] = array('between', array($beginToday, $endToday));


            $totaluser_day = (new User())->where($map)->count();
            $totalshop_day = 0;
            $totalacti_day = (new Activity())->where($map)->count();
        } else {
            $totaluser = (new User())->count();
            $totalshop = (new StoreShop())->count();
            $totalactivity = (new Activity())->count();

            $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
            $map['createtime'] = array('between', array($beginToday, $endToday));

            $totaluser_day = (new User())->where($map)->count();
            $totalshop_day = (new StoreShop())->where($map)->count();
            $totalacti_day = (new Activity())->where($map)->count();

        }

        return compact(
            'totaluser',
            'totalshop',
            'totalactivity',
            'totaluser_day',
            'totalshop_day',
            'totalacti_day');

    }


    public function getAgentKpi($uid)
    {
        $cdk = Cache::get('cdk');
        if ($cdk) {
            //使用次数
            $times = $cdk['server']['composetimes'] - $cdk['server']['usedtimes'];
        }

        if ($uid != get_addon_config('douyin')['super_id']) {
            $child = $this->getAgentChild($uid, 'shop_id');
            if ($child) {
                $map['manystore_id'] = ['in', $child];
            } else {
                $shop_id = $this->where(['id' => $uid])->value('shop_id');
                $map['manystore_id'] = $shop_id;
            }
            $totaluser = (new User())->where($map)->count();
            $totalshop = 0;
            $totalactivity = (new Activity())->where($map)->count();

            $activity_ids = (new Activity())->where($map)->column('id');

            $total_play = (new SellerLog())->where('activity_id', 'in', $totalactivity)->sum('total_play');

            $total_checkall = (new SellerCode())->where('activity_id', 'in', $totalactivity)->count();
            $total_check = (new SellerCode())->where('activity_id', 'in', $totalactivity)->where('status', 1)->count();

            $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
            $map['createtime'] = array('between', array($beginToday, $endToday));


            $totaluser_day = (new User())->where($map)->count();
            $totalshop_day = 0;
            $totalacti_day = (new Activity())->where($map)->count();
        } else {
            $totaluser = (new User())->count();
            $totalshop = (new StoreShop())->count();
            $totalactivity = (new Activity())->count();

            $total_play = (new SellerLog())->sum('total_play');
            //领劵数
            $total_checkall = (new SellerCode())->count();

            //核销数
            $total_check = (new SellerCode())->where('status', 1)->count();

            $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
            $map['createtime'] = array('between', array($beginToday, $endToday));

            $totaluser_day = (new User())->where($map)->count();
            $totalshop_day = (new StoreShop())->where($map)->count();
            $totalacti_day = (new Activity())->where($map)->count();

        }

        return compact(
            'times',
            'totaluser',
            'totalshop',
            'totalactivity',
            'totaluser_day',
            'totalshop_day',
            'totalacti_day',
            'total_play',
            'total_check',
            'total_checkall'
        );

    }

    public function getDayKpi($uid, $value, $time)
    {


        if ($uid != get_addon_config('douyin')['super_id']) {
            $child = $this->getAgentChild($uid, 'shop_id');
            if ($child) {
                $map['manystore_id'] = ['in', $child];
            } else {
                $shop_id = $this->where(['id' => $uid])->value('shop_id');
                $map['manystore_id'] = $shop_id;
            }
        } else {
            $map = null;
        }

        if ($value) {
            $map['manystore_id'] = $value;
        }

        $where=null;
        $totaluser = (new User())->where($map)->count();
        $totalshop = 0;
        $totalactivity = (new Activity())->where($map)->count();

        $activity_ids = (new Activity())->where($map)->column('id');


        switch ($time) {
            case 1:$beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
                $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
                $where['createtime'] = array('between', array($beginToday, $endToday));break;

            case 2:$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
                   $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
                   $where['createtime'] = array('between', array($beginYesterday,$endYesterday));break;

            case 3:$beginToday = strtotime("-1 week");
                $endToday =time();
                $where['createtime'] = array('between', array($beginToday, $endToday));break;
        }



        $total_play = (new SellerLog())->where('activity_id', 'in', $totalactivity)->where($where)->sum('total_play');
        $total_like = (new SellerLog())->where('activity_id', 'in', $totalactivity)->where($where)->sum('total_like');
        $total_comment = (new SellerLog())->where('activity_id', 'in', $totalactivity)->where($where)->sum('total_comment');
        $total_share = (new SellerLog())->where('activity_id', 'in', $totalactivity)->where($where)->sum('total_share');
        $total_num = (new SellerLog())->where('activity_id', 'in', $totalactivity)->where($where)->count();


     /*   $total_checkall = (new SellerCode())->where('activity_id', 'in', $totalactivity)->count();
        $total_check = (new SellerCode())->where('activity_id', 'in', $totalactivity)->where('status', 1)->count();*/






        return compact(
            'times',
            'totaluser',
            'totalshop',
            'totalactivity',
            'totaluser_day',
            'total_num',
            'total_like',
            'total_play',
            'total_comment',
            'total_share'
        );

    }

    //获取代理商系统名称
    public function getAgentSystemName()
    {
        if (empty(session('admin.agent_system_name'))) {
            $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
            if ($host) {
                $agentSystemName = $this->where('agent_domain', $host)->value('agent_system_name');
                session('admin.agent_system_name', $agentSystemName);
            }
        }

        return session('admin.agent_system_name');
    }

}
